package com.tanhua.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.db.mapper.BlackListMapper;
import com.tanhua.db.mapper.UserInfoMapper;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.dto.RecommendUserDto;
import com.tanhua.model.pojo.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlackListMapper blackListMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        UserInfo userInfo = userInfoMapper.selectById(id);
        return userInfo;
    }

    //分页展示黑名单
    @Override
    public Page<UserInfo> findBlackList(Long userId, Integer page, Integer pagesize) {
        Page<UserInfo> infoPage = new Page<>(page,pagesize);
        infoPage = blackListMapper.findBlackList(infoPage,userId);
        return infoPage;
    }

    @Override
    public Page<UserInfo> findByDto(List<Long> ids, RecommendUserDto recommendUserDto) {
        //1.用户id列表
        LambdaQueryWrapper<UserInfo> queryWrapper = Wrappers.lambdaQuery(UserInfo.class).in(UserInfo::getId,ids);
        //2.添加筛选条件
        if (recommendUserDto != null) {
            queryWrapper.lt(recommendUserDto!=null,UserInfo::getAge,recommendUserDto.getAge());
            queryWrapper.eq(StringUtils.isNotBlank(recommendUserDto.getGender()),UserInfo::getGender,recommendUserDto.getGender());
        }

        Page<UserInfo> pages =  new Page<>(recommendUserDto.getPage(), recommendUserDto.getPagesize());
        return userInfoMapper.selectPage(pages,queryWrapper);
    }

    //根据ids查询用户详细信息
    @Override
    public List<UserInfo> findByIds(List<Long> ids) {
        return userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class).in(UserInfo::getId,ids));
    }

    //查询好友信息集合
    @Override
    public Page<UserInfo> findInfoPage(List<Long> ids, Integer page, Integer pagesize, String keyword) {
        //封装分页对象
        Page<UserInfo> pages = new Page<>(page,pagesize);
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserInfo::getId,ids);
        queryWrapper.like(StringUtils.isNotBlank(keyword),UserInfo::getNickname,keyword);

        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(pages, queryWrapper);
        return userInfoPage;
    }

    //根据ids和性别查询用户信息
    @Override
    public List<UserInfo> findByIdsGender(List<Long> ids, String gender) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(UserInfo::getId,ids);
        queryWrapper.eq(StringUtils.isNotBlank(gender),UserInfo::getGender,gender);
        return userInfoMapper.selectList(queryWrapper);
    }

    //分页查询
    @Override
    public Page<UserInfo> pages(Integer page, Integer pagesize) {
        Page pages = new Page(page,pagesize);
        return userInfoMapper.selectPage(pages,null);
    }
}
